﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Read.Application.Command.Fram;
using Traceability.App.Domain.养殖基本信息表;
using Traceability.App.Domain.新文件夹;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Fram
{
    public class CreateFramHandler : IRequestHandler<CreateFramCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<FarmModel> farmRepo;
        private readonly IMapper mapper;

        public CreateFramHandler(IBaseRepository<FarmModel> farmRepo,IMapper mapper)
        {
            this.farmRepo = farmRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(CreateFramCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var trace = farmRepo.GetAll().Where(x =>x.FeedBrand==request.FeedBrand);
            var entity = mapper.Map<FarmModel>(request);
            var r = farmRepo.Add(entity).Result;
            if (r > 0)
            {
                res.Message = "新增成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "新增失败";
                res.Code = ApiEnum.Error;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
